package dyyx;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

public class App {

	private static final int KEY_COUNT = 100000000;

	private static final Random RAND = new Random();

	private static final Map<String, String> map = new ConcurrentHashMap<>();

	private static final int VALUE_LEN = 100000;

	public static void main(String[] args) throws Exception {

		System.out.println("App start "+LocalDateTime.now());
		
		int num = 0;
		while (true) {
			try {
				num++;
				map.put(genKey(), genValue());

				if (num > 0 & num % 100 == 0) {
					Thread.yield();
				}
				
				if(num > KEY_COUNT){
					num = 0;
				}

			} catch (Throwable e) {
				if (e instanceof OutOfMemoryError) {					
					System.out.println("OutOfMemoryError and clear,size="+map.size()+","+LocalDateTime.now());
					map.clear();
				}
			}
		}
	}

	private static String genKey() {
		return "key-" + RAND.nextInt(KEY_COUNT);
	}

	private static String genValue() {
		StringBuilder sb = new StringBuilder(VALUE_LEN + 64);
		sb.append(LocalDateTime.now().toString()).append("_");

		for (int i = 0; i < VALUE_LEN; i++) {
			sb.append("X");
		}
		return sb.toString();
	}
}
